package com.amkai.aes.webservice.mobile;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.amkai.aes.webservice.mobile.objecthandler.MobileLoginJobHandler;
import com.amkai.aes.webservice.mobile.objecthandler.MobileOrganizationJobHandler;
import com.amkai.aes.webservice.mobile.objecthandler.MobileSchedulerJobHandler;
import com.amkai.mobile.common.job.MobileJob;
import com.amkai.mobile.common.job.MobileJobResult;
import com.amkai.mobile.common.job.login.MobileLoginJob;
import com.amkai.mobile.common.job.organization.MobileOrganizationJob;
import com.amkai.mobile.common.job.scheduler.MobileSchedulerJob;

public class MobileJobManager {

	@SuppressWarnings("unchecked")
	private Map<Class<? extends MobileJob>, Class<? extends MobileJobObjectHandler>> objectHandlers = new HashMap<Class<? extends MobileJob>, Class<? extends MobileJobObjectHandler>>();
	
	private static MobileJobManager instance = new MobileJobManager();
	
	public MobileJobManager() {
		registerObjectHandlers();
		System.out.println("::Set the JobManagerInterface to MemoryLoggerJob.");
		//MemoryLoggerJob.setJobManagerInterface(this);
	}
	private void registerObjectHandlers() {
		registerObject(MobileLoginJob.class, MobileLoginJobHandler.class);
		registerObject(MobileOrganizationJob.class, MobileOrganizationJobHandler.class);
		registerObject(MobileSchedulerJob.class, MobileSchedulerJobHandler.class);
		
	}
	public static MobileJobResult processJob(MobileJob mobileJob) throws Exception {
		// load configuration properties
		if (!MobileServerConfigManager.isMobileConfigurationLoaded()) {
			MobileServerConfigManager.loadConfigurationFile();
		}
		// TODO Auto-generated method stub
		return instance.process(mobileJob);
		
	}
	
	@SuppressWarnings("unchecked")
	private MobileJobResult process(MobileJob mobileJob) throws Exception {
		
		Class objectHandlerClass = objectHandlers.get(mobileJob.getClass());
		MobileJobObjectHandler mjoh = (MobileJobObjectHandler) objectHandlerClass.newInstance();
		return mjoh.process(mobileJob);
	}
	
	
	@SuppressWarnings("unchecked")
	private void registerObject(Class<? extends MobileJob> clazz, Class<? extends MobileJobObjectHandler>  objectHandler) {
		objectHandlers.put(clazz, objectHandler);
	}
	
	
	


}
